<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <title>添加新商品</title>
</head>
<body>
<div id="app">
    <el-page-header style="background-color:#0095d7;color: white;line-height: 60px " @back="goBack" content="添加商品">
    </el-page-header>
    <el-divider></el-divider>
    <el-card>
        <el-form style="width: 400px;margin: 30px auto" label-width="80px">
            <el-form-item label="商品名">
                <el-input type="text" placeholder="请输入商品名" v-model="p.title"></el-input>
            </el-form-item>
            <el-form-item label="价格">
                <el-input type="text" placeholder="请输入商品价格" v-model="p.price"></el-input>
            </el-form-item>
            <el-form-item label="原价">
                <el-input type="text" placeholder="请输入商品原价" v-model="p.oldPrice"></el-input>
            </el-form-item>
            <el-form-item label="库存">
                <el-input type="text" placeholder="请输入商品库存" v-model="p.num"></el-input>
            </el-form-item>
            <el-form-item label="销量">
                <el-input type="text" placeholder="请输入商品销量" v-model="p.salCount"></el-input>
            </el-form-item>
            <el-form-item label="商品分类">
                <el-select placeholder="请选择" v-model="p.categoryId">
                    <el-option v-for="c in categoryArr" :label="c.name" :value="c.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="商品图片">
                <el-upload
                        action="/upload"
                        :limit="1"
                        name="picFile"
                        list-type="picture-card"
                        :on-preview="handlePictureCardPreview"
                        :on-remove="handleRemove"
                        :on-success="handleSuccess"
                >
                    <i class="el-icon-plus"></i>
                </el-upload>
                <el-dialog :visible.sync="dialogVisible">
                    <img width="100%" :src="dialogImageUrl" alt="">
                </el-dialog>
            </el-form-item>
            <el-form-item>
                <el-button @click="insert()">发布商品</el-button>
            </el-form-item>
        </el-form>


    </el-card>
</div>
</body>
<!-- import Vue before Element -->
<script src="https://unpkg.com/vue@2.6.14/dist/vue.min.js"></script>
<!-- import JavaScript -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script>
   let v = new Vue({
        el: '#app',
        data: function() {
            return {
                dialogImageUrl: '',
                dialogVisible: false,
                categoryArr:[],
                p:{id:"", goodsName:"",listPrice:"",sales:"",pictures:"",categoryName:""}
            }
        },
       methods:{
           goBack() {
               //返回上一页面
               history.back();
           },
           handleRemove(file, fileList) {
               console.log(file, fileList);
               let filename = file.response;
               axios.get("/remove?filename="+filename).then(function (response){
                   console.log("删除完成！")
               })
           },
           handlePictureCardPreview(file) {
               this.dialogImageUrl = file.url;
               this.dialogVisible = true;
           },
           insert(){
               if(v.p.url==""){
                   v.$message.error("请选择上传的图片！")
                   return;
               }
               axios.post("/product/insert",v.p).then(function (){
                   location.href="/admin.html";
               })
           },
           handleSuccess(response, file, fileList){
               console.log("上传文件成功！");
               console.log("response="+response);
               console.log("file="+file);
               v.p.url = response;
           }
       }
    })
</script>
</html>